package com.lib.base.router.interceptor

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.lib.base.data.http.LoginException
import com.lib.base.router.ARouterUtils
import com.lib.base.router.app.AppPath
import com.lib.base.router.xsos.XSPath
import com.lib.base.utils.data.SPUtils

@Interceptor(priority = 10, name = "LoginInterceptor")
class LoginInterceptor : IInterceptor {
    /**
     * The operation of this interceptor.
     *
     * @param postcard meta
     * @param callback cb
     */
    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {

        var isLogin = SPUtils.getBoolean("isLogin", false)

        if (null != postcard && postcard.extra == 101) {

            if (isLogin) {
                callback!!.onContinue(postcard)

            } else {
                callback!!.onInterrupt(LoginException(101, "请先登录"))
                ARouterUtils.build(XSPath.LOGIN).greenChannel().navigation()
            }

        }
    }

    /**
     * Do your init work in this method, it well be call when processor has been load.
     *
     * @param context ctx
     */
    override fun init(context: Context?) {
    }
}